export default {
    formatTableData(oData,pageNumber,pageSize) {
        //筛选表头
        // oData.columns = oData.columns.filter(item => {
        //     return item.name !== 'id'
        // });
        //形成表头数组
        let columnNameArr = oData.columns && oData.columns.map(item => {
            return item.field;
        });
        //将表头与数组形式的数据，转为element能接受的数据形式
        oData.data = oData.data && oData.data.map((item, index) => {
            return formaterData(columnNameArr, item);
        });

        const total = oData.total;
        if(Math.ceil(total/pageSize) <=pageNumber){
            oData.isLastPage = true
        }else{
            oData.isLastPage = false
        }

        oData.pageInfo = this.pageHelper(total,pageNumber,pageSize)

        return oData
    },

    /*   (important:)分页   */
    pageHelper(total = 0, pageNumber = 1, pageSize = 10) {
        // page相关信息
        const pageInfo = {
            pageNumber, pageSize, total,
            currentPage: parseInt(pageNumber),
            nextPage: parseInt(pageNumber) + 1,
            isFirstPage: false,
            isLastPage: false,
            pageCount: 1,
        };
        // 是否第一页
        if (pageNumber === 1) {
            pageInfo.isFirstPage = true;
        }
        // 是否最后一页
        if (pageNumber * pageSize >= total) {
            pageInfo.isLastPage = true;
        }
        // 页码数
        pageInfo.pageCount = Math.ceil(total / pageSize);


        return pageInfo;

    },

    formatApiOptionForEdit(option, schoolIdList, yearArr) {

        // const optionItem = {
        //     tableId: null,
        //     pageSize: 0,
        //     filter: [],
        //     xue_xiao_id: [],
        //     group: ['xue_xiao_id'],
        //     columns: ['xue_xiao_id', 'dan_wei_ming_cheng'],
        // }
        if(yearArr[0] == null&&yearArr[1] == null){
            yearArr = [1992, 2019]
        }
        if(schoolIdList.length == 0){
            schoolIdList = [1]
        }

        let filter = {
            pageNumber: 0,
            pageSize: 0,
            xue_xiao_id: schoolIdList || [1],
            nian_du_qu_jian: yearArr ,
        }


        let filterArr = []
        this.objToStrMap(filter).forEach(function (value, key, map) {
            if (key != "pageNumber" && key != "pageSize" && value) {
                if (key == "name") {
                    filterArr.push([key, 6, value])
                }
                else if (key == "nian_du_qu_jian") {
                    if (value[0]) {
                        filterArr.push([key, 3, value[0]])
                    }
                    if (value[1]) {
                        filterArr.push([key, 5, value[1]])
                    }
                }
                else if (typeOfData(value) == "string") {
                    filterArr.push([key, 0, value])
                }
                else if (typeOfData(value) == "array") {
                    filterArr.push([key, 8, value])
                }
            }
        });

        let columnsStr = ""

        option.tableConfig.columns.forEach((item, index) => {
            let str = ''
            if (item.fun != "") {
                str = item.fun + ":" + item.name
            } else {
                str = item.name
            }

            columnsStr = columnsStr + str + ","
            // if (css !=  option.tableConfig.columns.length) {
            // } else {0
            //     columnsStr = columnsStr + str
            // }
        })

        let orderArr = []
        orderArr = option.tableConfig.order.map((item, index) => {
            if (item.fun != "") {
                return [item.fun + ":" + item.name, item.sort]
            } else {
                return [item.name, item.sort]
            }
        })

        let defaultOption = {
            columns: columnsStr,
            filter: filterArr,
            order: orderArr || [],
            // pageNumber: filter.pageNumber || 0,
            pageSize: filter.pageSize || 0,
            group: option.tableConfig.group || [],
        }
        return defaultOption
    },


    formatApiOptionForCommon(option) {
        let filterArr = []
        this.objToStrMap(option).forEach(function (value, key, map) {
            if (key != "pageNumber" && key != "pageSize" && key != "columns" && key != "group" && key != "filter" && key != "order" && key != "tableId" && value) {
                if (key == "name") {
                    filterArr.push([key, 6, value])
                } else if (key == "nian_du_qu_jian") {
                    if (value[0]) {
                        filterArr.push([key, 3, value[0]])
                    }
                    if (value[1]) {
                        filterArr.push([key, 5, value[1]])
                    }
                } else if (typeOfData(value) == "string") {
                    filterArr.push([key, 0, value])
                } else if (typeOfData(value) == "array") {
                    filterArr.push([key, 8, value])
                }
            }
        });

        filterArr = filterArr.concat(option.filter)

        let columnsStr = ""
        if (option.columns) {
            option.columns.map((item, index) => {
                if (index != option.columns.length) {
                    columnsStr = columnsStr + item + ","
                } else {
                    columnsStr = columnsStr + item
                }
            });
        }

        let defaultOption = {
            columns: columnsStr,
            filter: filterArr,
            order: option.order || [],
            pageNumber: option.pageNumber || 0,
            pageSize: option.pageSize || 0,
            group: option.group || [],
        }

        return defaultOption
    },

    formatApiOption(option) {
        let filterArr = option.filter||[]
        this.objToStrMap(option).forEach(function (value, key, map) {
            if (key != "pageNumber" && key != "pageSize" && key != "columns" && key != "group" && key != "filter" && key != "order" && key != "tableId" && value) {
                if (key == "name"||key == "guanjianci"||key == "guan_jian_ci"||key == "zheng_ce_ming_cheng"||key == "lun_wen_biao_ti"||key == "ming_cheng"||key == "xing_ming"||key == "xue_xiao_ming_cheng"
                    ||key == "lun_wen_biao_ti"||key == "zhuan_li_ming_cheng"
                    // ||key == "keyword"
                    ||key == "kan_wu__hui_yi_ri_qi"||key == "huo_jiang_ming_cheng" || key == "huo_jiang_shi_jian" || key == "yan_jiu_fang_xiang"
                    ||key == "zhuan_zhu_ming_cheng"||key == "ruan_zhu_ming_cheng"||key == "ceng_ci") {
                    filterArr.push([key, 6, value])
                } else if (key == "nian_du_qu_jian") {
                    if (value[0]) {
                        filterArr.push([key, 3, value[0]])
                    }
                    if (value[1]) {
                        filterArr.push([key, 5, value[1]])
                    }
                } else if (typeOfData(value) == "string") {
                    filterArr.push([key, 0, value])
                } else if (typeOfData(value) == "array") {
                    filterArr.push([key, 8, value])
                }
            }
        });

        let columnsStr = ""
        if (option.columns) {
            option.columns.map((item, index) => {
                if (index != option.columns.length) {
                    columnsStr = columnsStr + item + ","
                } else {
                    columnsStr = columnsStr + item
                }
            });
        }


        let defaultOption = {
            columns: columnsStr,
            filter: filterArr,
            order: option.order || [],
            pageNumber: option.pageNumber || 0,
            pageSize: option.pageSize|option.pageSize===0?option.pageSize : 10,
            group: option.group || [],
        }
        return defaultOption
    },

    objToStrMap(obj) {
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            strMap.set(k, obj[k]);
        }
        return strMap;
    },
    isValidYear(year) {
        if (!year) return false
        if (typeof year == 'string') {
            if (year.length != 4) {
                return false
            } else {
                year = year * 1
            }
        }
        if (typeof year == 'number') {
            if (year < 2000) {
                return false
            }
        }
        return true
    }
}


function typeOfData(data) {
    if ((typeof data == 'object') && data.constructor == Array) {
        return "array"
    } else {
        return "string"
    }
}

//格式化表头信息
function formaterData(list, values) {
    var result = {};
    for (var i = 0; i < list.length; i++) {
        result[list[i]] = values[i];
    }
    return result;
}
